{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<bound method Counter.elements of Counter({1: 2, 2: 1, 3: 1})>\n"
     ]
    }
   ],
   "source": [
    "import collections\n",
    "\n",
    "x = [1,2,3,4,1,3,2]\n",
    "y = [1,2,1,3]\n",
    "c = collections.Counter(x)\n",
    "d = collections.Counter(y)\n",
    "z = c&d\n",
    "print(z.elements)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20.0 10.0 120.0\n"
     ]
    }
   ],
   "source": [
    "from scipy.special import perm, comb, factorial # 排列\n",
    "print(perm(5,2) # 20\n",
    ",comb(5,2) # 10\n",
    ",factorial(5)) # 120\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "def superEggDrop(k: int, n: int) -> int:\n",
    "    if n == 1:\n",
    "        return 1\n",
    "    f = [[0] * (k + 1) for _ in range(n + 1)]\n",
    "    for i in range(1, k + 1):\n",
    "        f[1][i] = 1\n",
    "    ans = -1\n",
    "    for i in range(2, n + 1):\n",
    "        for j in range(1, k + 1):\n",
    "            f[i][j] = 1 + f[i - 1][j - 1] + f[i - 1][j]\n",
    "        if f[i][k] >= n:\n",
    "            ans = i\n",
    "            break\n",
    "    return ans\n",
    "\n",
    "superEggDrop(2,9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.169925001442312"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "np.log2(9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "fuck\n"
     ]
    }
   ],
   "source": [
    "x = {\"k\":1}\n",
    "for i ,v in x.items():\n",
    "    print(\"fuck\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'[]'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str([])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'>\n"
     ]
    }
   ],
   "source": [
    "class dummy():\n",
    "    def __init__(self,input):\n",
    "        self.input = input\n",
    "    def __new__(cls, self):\n",
    "        return 1\n",
    "    def output(self):\n",
    "        return self.input\n",
    "dummy_ = dummy(\"yes\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Singleton(object):\n",
    "    _instance = None\n",
    "\n",
    "    def __new__(cls, *args, **kwargs):\n",
    "        if cls._instance is None:\n",
    "            cls._instance = object.__new__(cls, *args, **kwargs)\n",
    "        return cls._instance\n",
    "\n",
    "single = Singleton()\n",
    "single_1 = Singleton()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<__main__.Singleton at 0x21a7e195910>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "single"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<__main__.Singleton at 0x21a7e195910>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "single_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 9, 8, 7, 6, 5, 4, 3]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(10,2,-1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "#定义一个元类\n",
    "class FirstMetaClass(type):\n",
    "    # cls代表动态修改的类\n",
    "    # name代表动态修改的类名\n",
    "    # bases代表被动态修改的类的所有父类\n",
    "    # attr代表被动态修改的类的所有属性、方法组成的字典\n",
    "    def __new__(cls, name, bases, attrs):\n",
    "        # 动态为该类添加一个name属性\n",
    "        attrs['name'] = \"C语言中文网\"\n",
    "        attrs['say'] = lambda self: print(\"调用 say() 实例方法\")\n",
    "        return super().__new__(cls,name,bases,attrs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'NoneType' object has no attribute 'name'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-13-e5d9847f4f8f>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      2\u001b[0m     \u001b[1;32mpass\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mc\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcla\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mc\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'name'"
     ]
    }
   ],
   "source": [
    "def cla(objcet, metaclass = FirstMetaClass):\n",
    "    pass\n",
    "c = cla()\n",
    "c.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "执行do\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "'NoneType' object is not callable",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-11-1f0a80dbeb6f>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     12\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34mf\"执行{do.__name__}\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     13\u001b[0m     \u001b[0mtime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 14\u001b[1;33m \u001b[0mtimer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdo\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-11-1f0a80dbeb6f>\u001b[0m in \u001b[0;36mtimer\u001b[1;34m(func)\u001b[0m\n\u001b[0;32m      3\u001b[0m     \u001b[1;31m# def run_time():\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m     \u001b[0mstart\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtime\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m     \u001b[0mfunc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      6\u001b[0m     \u001b[0mend\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtime\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34mf\"执行函数{func.__name__},用时{end-start}秒\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: 'NoneType' object is not callable"
     ]
    }
   ],
   "source": [
    "import time\n",
    "def timer(func):\n",
    "    # def run_time():\n",
    "    start = time.time()\n",
    "    func()\n",
    "    end = time.time()\n",
    "    print(f\"执行函数{func.__name__},用时{end-start}秒\") \n",
    "    # return run_time\n",
    "\n",
    "\n",
    "def do():\n",
    "    print(f\"执行{do.__name__}\")\n",
    "    time.sleep(2)\n",
    "timer(do())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'func'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def func():\n",
    "    pass\n",
    "\n",
    "func.__name__\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "执行结束,执行时间为： 0:00:03.011593\n"
     ]
    }
   ],
   "source": [
    "import datetime\n",
    "def take_up_time(func):\n",
    "    def run_time():\n",
    "        start_time = datetime.datetime.now()\n",
    "        func() # 执行被装饰的函数\n",
    "        end_time = datetime.datetime.now()\n",
    "        print('执行结束,执行时间为：', end_time - start_time)\n",
    "    return run_time\n",
    "@take_up_time\n",
    "def test():\n",
    "    for i in range(3):\n",
    "        time.sleep(1)\n",
    "\n",
    "test()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'tuple'>\n",
      "dict_keys(['fuck'])\n",
      "dict_values([1])\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "def test(*arg, **kwarg):\n",
    "    print(type(arg))\n",
    "    print(kwarg.keys())\n",
    "    print(kwarg.values())\n",
    "    print(type(kwarg))\n",
    "\n",
    "test(12,2321,fuck=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution:\n",
    "    def generateParenthesis(self, n: int) -> List[str]:\n",
    "        if n == 1:\n",
    "            return list({'()'})\n",
    "        res = set()\n",
    "        for i in self.generateParenthesis(n - 1):\n",
    "            for j in range(len(i) + 2):\n",
    "                res.add(i[0:j] + '()' + i[j:])\n",
    "        return list(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:root:bug!!!!!!!\n"
     ]
    }
   ],
   "source": [
    "import logging\n",
    "logging.info(\"small \")\n",
    "logging.warning(\"bug!!!!!!!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "__main__.A"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class A():\n",
    "    var = 1\n",
    "\n",
    "getattr(A, \"var1\", 3)\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "b3527e6919fb3c5de6d1946832ad613724bb5bc6752cd9e3fcbde93ba38fe29f"
  },
  "kernelspec": {
   "display_name": "Python 3.9.7 64-bit (conda)",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
